using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace Wmb.Web {
    /// <summary>
    /// The BetterImageProcessorFeatureReceiver inherist SPFeatureReceiver to (un)register the web.config modifications for the BetterImageProcessor with SharePoint
    /// </summary>
    [CLSCompliant(false)]
    public class BetterImageProcessorFeatureReceiver : SPFeatureReceiver {
        /// <summary>
        /// Contains the Owner for all Web Config Modifications
        /// </summary>
        private const string Owner = "BetterImageProcessor";

        /// <summary>
        /// The Web Config Modifications that are to be applied to the web applications web.config where this feature is activated
        /// </summary>
        private static readonly SPWebConfigModification[] modifications = {
            new SPWebConfigModification("section[@name=\"BetterImageConfiguration\"][@type=\"Wmb.Web.BetterImageConfigurationSection, Wmb.Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d67add552101f90a\"]", "configuration/configSections")
                { Owner = Owner, Sequence = 0, Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode, 
                  Value = "<section name=\"BetterImageConfiguration\" type=\"Wmb.Web.BetterImageConfigurationSection, Wmb.Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d67add552101f90a\" />" },
            new SPWebConfigModification("BetterImageConfiguration", "configuration")
                { Owner = Owner, Sequence = 0, Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode, Value = "<BetterImageConfiguration imageRetriever=\"SPImageRetreiver\"><imageRetrievers><add name=\"SPImageRetreiver\" class=\"Wmb.Web.SPImageRetriever, Wmb.Web.SharePoint, Version=1.0.0.0, Culture=neutral, PublicKeyToken=e8320c2cca9d9a46\" /></imageRetrievers></BetterImageConfiguration>" },
            new SPWebConfigModification("add[@verb=\"GET\"][@path=\"ImageHandler.ashx\"][@type=\"Wmb.Web.ImageHandler, Wmb.Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d67add552101f90a\"]", "configuration/system.web/httpHandlers")
                { Owner = Owner, Sequence = 0, Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode, Value = "<add verb=\"GET\" path=\"ImageHandler.ashx\" type=\"Wmb.Web.ImageHandler, Wmb.Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d67add552101f90a\"/>" }
        };

        /// <summary>
        /// Runs when the feature is activated.
        /// </summary>
        /// <param name="properties">The properties.</param>
        public override void FeatureActivated(SPFeatureReceiverProperties properties) {
            SPWebApplication webApp = (SPWebApplication)properties.Feature.Parent;

            foreach (SPWebConfigModification modification in modifications) {
                webApp.WebConfigModifications.Add(modification);
            }

            webApp.Update();
            webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
        }

        /// <summary>
        /// Runs when the feature is deactivating.
        /// </summary>
        /// <param name="properties">The properties.</param>
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties) {
            SPWebApplication webApp = (SPWebApplication)properties.Feature.Parent;

            List<SPWebConfigModification> modifications = webApp.WebConfigModifications.ToList();

            foreach (SPWebConfigModification mod in modifications) {
                if (mod.Owner == Owner) {
                    webApp.WebConfigModifications.Remove(mod);
                }
            }

            webApp.Update();
            webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
        }

        /// <summary>
        /// Runs when the feature is installed.
        /// </summary>
        /// <param name="properties">The properties.</param>
        public override void FeatureInstalled(SPFeatureReceiverProperties properties) {
            // throw new Exception("The method or operation is not implemented.");
        }

        /// <summary>
        /// Runs when the feature is uninstalling.
        /// </summary>
        /// <param name="properties">The properties.</param>
        public override void FeatureUninstalling(SPFeatureReceiverProperties properties) {
            // throw new Exception("The method or operation is not implemented.");
        }
    }
}
